package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.entity.Intention;
import tech.aistar.entity.Message;
import tech.aistar.entity.SeeRoomTrend;
import tech.aistar.entity.Tenant;
import tech.aistar.service.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/intention")
public class IntentionController {
    @Autowired
    private IIntentionService intentionService;
    @Autowired
    private ITenantService tenantService;
    @Autowired
    private IMessageService messageService;

    @Autowired
    private ISeeRoomTrendService seeRoomTrendService;
    @Autowired
    private IImageService iImageService;
    @Autowired
    private IHouseService iHouseService;
    @GetMapping("/TenantIntention/{tenantId}")
    public String selectAll(HttpServletRequest request,@PathVariable("tenantId") Integer tenantId) {
//        String tenantId = request.getParameter("tenantId");
//        intentionService.insertTenantId(Integer.valueOf(tenantId));
        Intention intention = intentionService.selectById(Integer.valueOf(tenantId));
        request.setAttribute("intention", intention);
        Tenant tenant = tenantService.selectById(Integer.valueOf(tenantId));
        request.setAttribute("tenant", tenant);
        List<Message> messageStates = messageService.selectByState(tenant.getRegisterId());
        request.setAttribute("messageSize",messageStates.size());
        List<SeeRoomTrend> seeRoomTrends=seeRoomTrendService.selectThree();
        int i=0;
        for (SeeRoomTrend seeRoomTrend : seeRoomTrends) {
            i++;
            request.setAttribute("image"+i, iImageService.selectOneByHouseId(seeRoomTrend.getHouseId()));
            request.setAttribute("house"+i,iHouseService.selectByhouseId(seeRoomTrend.getHouseId()));
        }
        return "/personal_homepage/tenant/intention";

    }

    @GetMapping("/updateIntention/{tenantId}/{greenery}/{trafficConvenient}/{shoppingConvenient}/{schoolNearbyHouse}/{fridge}/{television}/{washingMachine}/{geyser}/{airCondition}/{broadband}/{sofa}/{bed}/{heat}/{wardrobe}/{cookingable}/{bathroom}/{balcony}/{intelligentLock}/{rangeHood}/{gasStove}")
    public String updateIntention(HttpServletRequest request, @PathVariable("tenantId") Integer tenantId,@PathVariable("greenery") String greenery,
                                  @PathVariable("trafficConvenient") String trafficConvenient,
                                  @PathVariable("shoppingConvenient") String shoppingConvenient,
                                  @PathVariable("schoolNearbyHouse") String schoolNearbyHouse,
                                  @PathVariable("fridge") String fridge,
                                  @PathVariable("television") String television,
                                  @PathVariable("washingMachine") String washingMachine,
                                  @PathVariable("geyser") String geyser,
                                  @PathVariable("airCondition") String airCondition,
                                  @PathVariable("broadband") String broadband,
                                  @PathVariable("sofa") String sofa,
                                  @PathVariable("bed") String bed,
                                  @PathVariable("heat") String heat,
                                  @PathVariable("wardrobe") String wardrobe,
                                  @PathVariable("cookingable") String cookingable,
                                  @PathVariable("bathroom") String bathroom,
                                  @PathVariable("balcony") String balcony,
                                  @PathVariable("intelligentLock") String intelligentLock,
                                  @PathVariable("rangeHood") String rangeHood,
                                  @PathVariable("gasStove") String gasStove) {
//        String fridge = request.getParameter("fridge");
//        String television = request.getParameter("television");
//        String airCondition = request.getParameter("airCondition");
//        String geyser = request.getParameter("geyser");
//        String washingMachine = request.getParameter("washingMachine");
//        String broadband = request.getParameter("broadband");
//        String sofa = request.getParameter("sofa");
//        String balcony = request.getParameter("balcony");
//        String gasStove = request.getParameter("gasStove");
//        String rangeHood = request.getParameter("rangeHood");
//        String wardrobe = request.getParameter("wardrobe");
//        String heat = request.getParameter("heat");
//        String bed = request.getParameter("bed");
//        String intelligentLock = request.getParameter("intelligentLock");
//        String greenery = request.getParameter("greenery");
//        String schoolNearbyHouse = request.getParameter("schoolNearbyHouse");
//        String cookingable = request.getParameter("cookingable");
//        String bathroom = request.getParameter("bathroom");
//        String trafficConvenient = request.getParameter("trafficConvenient");
//        String shoppingConvenient = request.getParameter("shoppingConvenient");
//        String tenantId = request.getParameter("tenantId");

        Intention intention = new Intention(Integer.valueOf(tenantId),
                greenery,
                trafficConvenient,
                shoppingConvenient,
                schoolNearbyHouse,
                fridge,
                television,
                washingMachine,
                geyser,
                airCondition,
                broadband,
                sofa,
                bed,heat,wardrobe,cookingable,bathroom,balcony,intelligentLock,rangeHood,gasStove);

        int count = intentionService.updateByTenantId(intention);

        return "redirect:/intention/TenantIntention/"+tenantId;
    }





    @GetMapping("/TenantDemand/{tenantId}")
    public String selectAllDemand(HttpServletRequest request,@PathVariable("tenantId") Integer tenantId) {
//        String tenantId = request.getParameter("tenantId");
        Intention intention = intentionService.selectById(Integer.valueOf(tenantId));
        request.setAttribute("intention", intention);
        Tenant tenant = tenantService.selectById(Integer.valueOf(tenantId));
        request.setAttribute("tenant", tenant);
        List<Message> messageStates = messageService.selectByState(tenant.getRegisterId());
        request.setAttribute("messageSize",messageStates.size());
        List<SeeRoomTrend> seeRoomTrends=seeRoomTrendService.selectThree();
        int i=0;
        for (SeeRoomTrend seeRoomTrend : seeRoomTrends) {
            i++;
            request.setAttribute("image"+i, iImageService.selectOneByHouseId(seeRoomTrend.getHouseId()));
            request.setAttribute("house"+i,iHouseService.selectByhouseId(seeRoomTrend.getHouseId()));
        }
        return "/personal_homepage/tenant/demand";
    }


    @GetMapping("/updateDemand/{tenantId}/{houseType}/{parkPlace}/{orientate}")
    public String updateDemand(HttpServletRequest request,@PathVariable("tenantId") Integer tenantId,
                               @PathVariable("houseType") String houseType,
                               @PathVariable("parkPlace") String parkPlace,
                               @PathVariable("orientate") String orientate) {
//        String houseType = request.getParameter("houseType");
//        String floor = request.getParameter("floor");
//        String parkPlace = request.getParameter("parkPlace");
//        String orientate = request.getParameter("orientate");
//        String tenantId = request.getParameter("tenantId");

        Intention intention = new Intention(Integer.valueOf(tenantId),houseType,parkPlace,orientate);

        int count = intentionService.updateDemandByTenantId(intention);

        return "redirect:/intention/TenantDemand/"+tenantId;
    }


}
